#!/usr/bin/env python3
# -*- coding:utf-8 -*-
## author : mike
## date   : 2014.12.4
"""
"""

import os 
import sys
from os.path import realpath, dirname, basename, isdir

sys.path.append(dirname(dirname(realpath(__file__))))
from xbc3.fileio import FileIO

class CountDifferentU(object):
    """docstring for CountDifferentU"""
    def __init__(self,*inputfiles):
        super(CountDifferentU, self).__init__()
        self.files_name = []
        for fn in inputfiles:
            if isdir(fn):
                continue
            if 'sys_ss' in  basename(fn):
                self.files_name.append(fn)
        if not self.files_name:
            print('Error:can not find sys_ss files!!!\n', file=sys.stderr)
            sys.exit()
        self.u_dict={}

    def cookie_ua(self, filename):
        """"""
        print(filename,"\n")
        file = FileIO(filename)
        for line in file:  
            try:
                times =int(line[83:line.find('|user')])
                if times == 1:
                    usagt =line[line.find('|user-agent:'):line.find('|referer:')] 
                    if usagt=='|user-agent:mozilla/5.0 (windows nt 5.1) applewebkit/537.36 (khtml like gecko) chrome/31.0.1650.63 safari/537.36':
           
                        if usagt in self.u_dict:
                            self.u_dict[usagt]+=1
                        else:
                            self.u_dict[usagt]=1
                else:
                    pass
            except Exception as e:
                pass

    def run(self):
        """"""
        for fn in self.files_name:
            self.cookie_ua(fn)        
        self.output()

    def output(self):
        """"""
        fpath=os.path.split(self.files_name[0])[0]
        result_path=os.path.join(fpath,'result.csv')
        
        with FileIO(result_path,'w') as f:
            f.write('user-agent,number\n')
            for k in sorted(self.u_dict, key=lambda x:self.u_dict[x], reverse=True):
                v = self.u_dict[k]
                f.write('%d, %s\n' % (v, k))

if __name__ == '__main__':
    '''driver'''
    inputfiles = []
    for line in sys.stdin:
        line = line.rstrip('\r\n')
        inputfiles.append(line)
    print(len(inputfiles))
    cud=CountDifferentU(*inputfiles)
    cud.run()